package com.scf.fruit.business.order.controller;

import com.scf.fruit.business.order.bo.OrderBo;
import com.scf.fruit.business.order.po.OrderInfoReq;
import com.scf.fruit.common.constants.MallConstants;
import com.scf.fruit.configuration.web.LoginValid;
import com.scf.fruit.enums.ExceptionCodeEnum;
import com.scf.fruit.util.CacheUtil;
import com.scf.fruit.util.ResponseVo;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.UUID;

/**
 * @author : 孙超飞
 * @title :
 * @description :
 * @copyright :
 * @date : 2018-05-21 15:21
 */
@Controller
@RequestMapping(value = "/order",produces = "application/json")
public class OrderController {
    @Resource
    private OrderBo orderBo;

    @RequestMapping(value = "/saveOrder",method = RequestMethod.POST)
    @ResponseBody
    @LoginValid
    public ResponseVo saveOrder(HttpServletRequest request, @RequestBody @Valid OrderInfoReq req){
        if(CollectionUtils.isEmpty(req.getList())){
            return new ResponseVo(ExceptionCodeEnum.EX_PARAM_ERROR.getValue(),ExceptionCodeEnum.EX_PARAM_ERROR.getMsg());
        }
        Long userid = Long.valueOf(request.getHeader("USERID"));
        String s = MallConstants.orderKey + "_" + userid;
        //防止重复提交
       if(null!=CacheUtil.get(s)){
           return new ResponseVo(ExceptionCodeEnum.EX_ORDER_SAVE_REPEAT.getValue(),ExceptionCodeEnum.EX_ORDER_SAVE_REPEAT.getMsg());
       }
        CacheUtil.put(s,UUID.randomUUID());
        ResponseVo responseVo = orderBo.saveOrder(userid, req);
        CacheUtil.remove(s);
        return responseVo;
    }

    @RequestMapping(value = "/deleteOrderDetail",method = RequestMethod.GET)
    @ResponseBody
    @LoginValid
    public ResponseVo deleteOrderDetail(HttpServletRequest request, @RequestParam("id")Long id){
        return orderBo.deleteOrderDetail(id,Long.valueOf(request.getHeader("USERID")));
    }

    @RequestMapping(value = "/deleteOrder",method = RequestMethod.GET)
    @ResponseBody
    @LoginValid
    public ResponseVo deleteOrder(HttpServletRequest request, @RequestParam("id")Long id){
        return orderBo.deleteOrder(id,Long.valueOf(request.getHeader("USERID")));
    }
}
